package com.atguigu.tingshu.album.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;
    @Autowired
    private VodUploadClient client;

    /**
     * 上传曲目
     *
     * @param file 文件
     * @return {@link Map }<{@link String }, {@link String }>
     */
    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {
        HashMap<String, String> map = new HashMap<>();
        VodUploadRequest request = new VodUploadRequest();
        String s = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);
        if (StringUtils.isNotEmpty(s)) {
            request.setMediaFilePath(s);
        }
        try {
            VodUploadResponse response = client.upload(vodConstantProperties.getRegion(), request);
            map.put("mediaFileId", response.getFileId());
            map.put("mediaUrl", response.getMediaUrl());
        } catch (Exception e) {
            // 业务方进行异常处理
            throw new GuiguException(400, "上传失败");
        }
        return map;

    }

    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String mediaFileId) {
        TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
        try {

            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());

            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] s = {mediaFileId};
            req.setFileIds(s);

            // 返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);
            MediaInfo[] mediaInfoSet = resp.getMediaInfoSet();

            if (mediaInfoSet.length > 0) {
                MediaInfo mediaInfo = mediaInfoSet[0];
                trackMediaInfoVo.setType(mediaInfo.getBasicInfo().getType());
                Long size = mediaInfo.getMetaData().getSize();
                trackMediaInfoVo.setSize(size);
                trackMediaInfoVo.setDuration(mediaInfo.getMetaData().getDuration());
                trackMediaInfoVo.setMediaUrl(mediaInfo.getBasicInfo().getMediaUrl());

            }
        } catch (
                TencentCloudSDKException e) {
            e.printStackTrace();
            throw new GuiguException(400, "获取声音异常");
        }
        return trackMediaInfoVo;
    }

    @Override
    public void removeMediaFile(String mediaFileIdBefore) {
        try {
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();


            client.DeleteMedia(req);


        } catch (TencentCloudSDKException e) {
            throw new GuiguException(400, "删除云端资源错误");

        }
    }
}
